
import Graphics from "./Graphics";
import Scenes from "./Scenes";
import Sprite, { SpriteTextType } from "./Sprite";
import FileManager from '../Manager/FileManager'
import Player from '../../example/Player'
import BaseManager from '../Manager/Index'
import VCanvas from "..";
let uid = 0;
class Stage {
    id: number;
    ctx: CanvasRenderingContext2D;
    graphics: Graphics;
    canvas: HTMLCanvasElement;
    sprite: Array<Sprite>;
    scenes: Array<Scenes>;

    parent: VCanvas;
    _scenes_background: { source: ImageData | ImageBitmap | HTMLCanvasElement, x: number, y: number, width: number, height: number };
    constructor(parent) {
        this.id = uid++;
        this.sprite = []
        this.canvas = document.createElement('canvas');
        this.canvas.width = 500;
        this.canvas.height = 500;
        this.ctx = this.canvas.getContext('2d');
        this.graphics = new Graphics(this.ctx);
        this.scenes = [];
        this.parent = parent;
        const leftCycle = {
            start: [this.start.bind(this)],
            update: [this.update.bind(this)]
        }
        BaseManager.addStageLifeCycle(this.id, leftCycle);
        this.init()
    }
    init() {
        const startScenes = new Scenes(this);
        this.scenes.push(startScenes)
        // const background = await FileManager.loadImage('/src/assets/1.jpg')
        startScenes.setScenes(null, 0, 0, 500, 500)
        let player = new Sprite(100, 100, 100, 100, this, Player)
        let enemy = new Sprite(200, 200, 100, 100, this, Player)

        player.setTextSprite('aaa', 0, 0, SpriteTextType.Fill);
        enemy.setTextSprite('bbb', 0, 0, SpriteTextType.Fill);
        startScenes.start()
    }
    start() {

        this.render()
    }

    addChild(sprite: Sprite) {
        this.sprite.push(sprite)
    }
    removeChild(sprite: Sprite) {
        this.sprite.splice(this.sprite.findIndex(item => {
            return item.id = sprite.id;
        }), 1)
    }

    update() {
        this.render()
    }
    render() {
        this.parent.ctx.clearRect(0, 0, 500, 500);
        this.parent.ctx.drawImage(this.canvas, 0, 0, 500, 500)
    }
}

export default Stage;